// page/unit/signUp/index.js
let config = require('../../../config.js');
const icon_err = "/image/err_w.png";
let app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    usid:'',
    sysNo:"",
    nickName:'',
    uname: "",
    upass: "",
    phone: "",
    orgName: "中国",
    jobTitle: "办公室",
    uflag: "",
    utype: "",
    orgsArr: ['中国', '省级', '市级', '区/县' , '街道'],
    orgIdx:'0',
    titlesArr: ['办公室', '行政部', '销售部', '测试部', '研发部'],
    titleIdx:'0',
    btnText:"确定"
  },
  bindOrgChange: function (e) {
    let orgsArr = this.data.orgsArr;
    this.setData({
      orgIdx: e.detail.value,
      orgName: orgsArr[e.detail.value]
    })
  },
  bindJobTitleChange: function (e) {
    let titlesArr = this.data.titlesArr;
    this.setData({
      titleIdx: e.detail.value,
      jobTitle: titlesArr[e.detail.value]
    })
  },
  showBigFlag:function(ev){},
  setUserFlag: function (ev) { },
  formSubmit:function(ev){
    let uinfo={
      usid : this.data.usid,
      uname: this.data.uname,
      upass: this.data.upass,
      nickName: this.data.nickName,
      phone: this.data.phone,
      orgName : this.data.orgName,
      jobTitle : this.data.jobTitle,
      uflag : this.data.uflag,
      utype : this.data.utype
    }
    // 判断资料是否齐全
    if (!uinfo.uname || !uinfo.upass || !uinfo.nickName || !uinfo.orgName || !uinfo.jobTitle || !uinfo.phone){
      wx.showToast({
        title: '请填写完整的资料',
        image: icon_err
      })
      return;
    }
    // console.log('wxaccount', uinfo);
    wx.request({
      url: config.loginUrl + 'wxaccount.php',
      // url: config.test + 'login/wxaccount.php',
      data:{
        uinfo:JSON.stringify(uinfo)
      },
      success:function(res){
        console.log('wxaccount',res);
        app.afterLogined(res.data);
        wx.switchTab({
          url:"/page/index/index"
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log('options',options);
    let that=this;
    if (options.usid){
      wx.setNavigationBarTitle({
        title: '完善账号信息',
      })
      wx.getUserInfo({
        success: function (res) {
          // console.log('getUserInfo',res);
          let userInfo = res.userInfo;
          that.setData({
            usid: options.usid,
            uflag: userInfo.avatarUrl,
            nickName: userInfo.nickName,
            utype:1,
            btnText:'确定'
          })
        }
      })
    }
  },
  //双向绑定表单输入元素
  inputNickName:function(event){
    this.setData({
      nickName: event.detail.value
    })
  },
  inputSysNo: function (event) {
    this.setData({
      sysNo: event.detail.value
    })
  },
  inputLoginName: function (event) {
    this.setData({
      uname: event.detail.value
    })
  },
  inputLoginPassWord: function (event) {
    this.setData({
      upass: event.detail.value
    })
  },
  inputPhone: function (event) {
    this.setData({
      phone: event.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})